package com.icebergteam.warfacewidget.core;

import java.util.Date;
import java.util.Locale;
import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.R;
import com.icebergteam.warfacewidget.core.utils.TimeDateUtils;
import com.icebergteam.warfacewidget.core.utils.ViewUtils;
import android.app.Notification;
import android.app.PendingIntent;
import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.View;
import android.widget.RemoteViews;

public class NotificationMessage extends ListViewItem{
	public static String className = NotificationMessage.class.getCanonicalName();
	private String smallContentText;
	private String title;
	private Bitmap image;
	private String bigContentText;
	private int number;
	private Date date;
	private int firstUnreadedItemIndex;
	private int id;
	private int defaults;
	private PendingIntent intent;
	private String tickerTitle;
	private Bitmap smallIcon;
	
	enum MESSAGE_STATE{
		MS_EXPANDED,
		MS_CUTTED
	};
	
	public NotificationMessage(){
		try {
			setNumber(1);
			setTitle("title");
			setDate(new Date());
			setSmallContentText("smallContentText");
			setBigContentText("bigContentText");
			setImage(ViewUtils.scaleImage((int)R.drawable.ic_launcher, 100));
			setFirstUnreadedItemIndex(0);
			setId(0);
			setDefaults(0);//Notification.DEFAULT_VIBRATE);
			setIntent(null);
			setTickerTitle("tickerTitle");
			setSmallIcon(ViewUtils.scaleImage((int)R.drawable.ic_launcher, 20));
		} catch (Exception e) {
			LogManager.LogEA(className, "error", e.getLocalizedMessage());
			e.printStackTrace();
		}
	}
	
	public NotificationMessage(Parcel in) {
		super(in);
	}


	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(this.bigContentText);
		dest.writeString(this.smallContentText);
		dest.writeString(this.title);
		dest.writeString(this.tickerTitle);
		dest.writeInt(this.number);
		dest.writeValue(this.image);
		dest.writeValue(this.smallIcon);
		dest.writeValue(this.date);
		dest.writeInt(this.firstUnreadedItemIndex);
		dest.writeInt(this.id);
		dest.writeInt(this.defaults);
		dest.writeValue(this.intent);
	}

	@Override
	public void readFromParcel(Parcel in) {
		setBigContentText(in.readString());
		setSmallContentText(in.readString());
		setTitle(in.readString());
		setTickerTitle(in.readString());
		setNumber(in.readInt());
		setImage((Bitmap) in.readValue(Bitmap.class.getClassLoader()));
		setSmallIcon((Bitmap) in.readValue(Bitmap.class.getClassLoader()));
		setDate((Date) in.readValue(Date.class.getClassLoader()));
		setFirstUnreadedItemIndex(in.readInt());
		setId(in.readInt());
		setDefaults(in.readInt());
		setIntent((PendingIntent) in.readValue(PendingIntent.class.getClassLoader()));
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * @return the smallContentText
	 */
	public String getSmallContentText() {
		return smallContentText;
	}

	/**
	 * @param smallContentText the smallContentText to set
	 */
	public void setSmallContentText(String smallContentText) {
		
		this.smallContentText = smallContentText;
	}
	
	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the pathImage
	 */
	public Bitmap getImage() {
		return image;
	}

	/**
	 * @param pathImage the pathImage to set
	 */
	public void setImage(Bitmap image) {
		this.image = image;
	}

	/**
	 * @return the bigContentText
	 */
	public String getBigContentText() {
		return bigContentText;
	}

	/**
	 * @param bigContentText the bigContentText to set
	 */
	public void setBigContentText(String bigContentText) {
		this.bigContentText = bigContentText;//trimString(bigContentText);
	}

	/**
	 * @return the number
	 */
	public int getNumber() {
		return number;
	}

	/**
	 * @param number the number to set
	 */
	public void setNumber(int number) {
		this.number = number;
	}

	/**
	 * @return the date
	 */
	public Date getDate() {
		return date;
	}

	/**
	 * @param date the date to set
	 */
	public void setDate(Date date) {
		this.date = date;
	}

	public static final Parcelable.Creator<NotificationMessage> CREATOR = new Parcelable.Creator<NotificationMessage>() {
		public NotificationMessage createFromParcel(Parcel in) {
			return new NotificationMessage(in);
		}

		public NotificationMessage[] newArray(int size) {
			return new NotificationMessage[size];
		}
	};
	
	public static RemoteViews getRemoteViews(NotificationMessage message, MESSAGE_STATE type){
		RemoteViews view = new RemoteViews(AppManager.getContext().getPackageName(), R.layout.notification_layout);
		try {
			view.setImageViewBitmap(R.id.notification_layout_imageview, message.getImage());
			view.setImageViewBitmap(R.id.notification_layout_imageview_logo, message.getSmallIcon());
		
			if(android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1){
				view.setViewVisibility(R.id.notification_layout_imageview, View.GONE);
			}
						
			view.setTextViewText(R.id.notification_layout_title, message.getTitle());
			
			switch(type){
			case MS_EXPANDED:
				view.setTextViewText(R.id.notification_layout_desc, message.getBigContentText());
				break;
			case MS_CUTTED:
				view.setTextViewText(R.id.notification_layout_desc, message.getSmallContentText());
				break;
			}
			
			String dateStr = (TimeDateUtils.compareToDay(new Date(),message.getDate()) == 0)? TimeDateUtils.getDateByLocale(message.getDate(), new Locale("RU"), "hh:mm"): TimeDateUtils.getDateByLocale(message.getDate(), new Locale("RU"), "dd.MM.yy hh:mm");
			view.setTextViewText(R.id.notification_layout_date, dateStr);
			
			view.setTextViewText(R.id.notification_layout_number, String.format("%s", message.getNumber()));
		} catch (Exception e) {
			LogManager.LogEA(className, "error", e.getLocalizedMessage());
			e.printStackTrace();
		}
		
		return view;
		
	}

	/**
	 * @return the firstUnreadedItemIndex
	 */
	public int getFirstUnreadedItemIndex() {
		return firstUnreadedItemIndex;
	}

	/**
	 * @param firstUnreadedItemIndex the firstUnreadedItemIndex to set
	 */
	public void setFirstUnreadedItemIndex(int firstUnreadedItemIndex) {
		this.firstUnreadedItemIndex = firstUnreadedItemIndex;
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the defaults
	 */
	public int getDefaults() {
		return defaults;
	}

	/**
	 * @param defaults the defaults to set
	 */
	public void setDefaults(int defaults) {
		this.defaults = defaults;
	}
	
	
	public Notification toBaseNotification (){
		RemoteViews smallContentView = NotificationMessage.getRemoteViews(
				this, MESSAGE_STATE.MS_CUTTED);

	
		
		Notification notif = new Notification();
		notif.flags |= Notification.FLAG_AUTO_CANCEL;

		if(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1){
			RemoteViews bigContentView = null;
			if(this.bigContentText != null){
				bigContentView = NotificationMessage.getRemoteViews(
						this, MESSAGE_STATE.MS_EXPANDED);
			}
			notif.bigContentView = bigContentView;
		}
		notif.contentView = smallContentView;
		notif.largeIcon = getImage();
		notif.tickerText = getTickerTitle();
		notif.icon = R.drawable.ic_launcher;
		notif.defaults = getDefaults();
		notif.contentIntent = getIntent();
		
		return notif;
	}

	/**
	 * @return the intent
	 */
	public PendingIntent getIntent() {
		return intent;
	}

	/**
	 * @param intent the intent to set
	 */
	public void setIntent(PendingIntent intent) {
		this.intent = intent;
	}

	/**
	 * @return the tickerTitle
	 */
	public String getTickerTitle() {
		return tickerTitle;
	}

	/**
	 * @param tickerTitle the tickerTitle to set
	 */
	public void setTickerTitle(String tickerTitle) {
		this.tickerTitle = tickerTitle;
	}

	/**
	 * @return the smallIcon
	 */
	public Bitmap getSmallIcon() {
		return smallIcon;
	}

	/**
	 * @param smallIcon the smallIcon to set
	 */
	public void setSmallIcon(Bitmap smallIcon) {
		this.smallIcon = smallIcon;
	}
}